"""
---------------------------------------------------------------------
- File: router.py
- Date: 2025/9/19
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 菜单管理路由
---------------------------------------------------------------------
"""

from fastapi import APIRouter, Depends
from app.common.app_response import UnifiedResponse
from typing import List
from .schemas import MenuTree, MenuIn, MenuOut
from .service import MenuService


router = APIRouter(
    prefix="/system_manage/menu",
    tags=["系统菜单管理"],
    # dependencies=[Depends(get_current_user)],
)

menu_service = MenuService()


@router.post(
    "/add_menu",
    summary="新增菜单",
    response_model=UnifiedResponse[MenuOut],
)
async def add_menu(menu_data: MenuIn):
    print("menu_data:", menu_data)
    menu = await menu_service.add_menu(menu_data)
    return UnifiedResponse(data=menu)


@router.delete(
    "/delete_menu/{menu_id}",
    summary="删除菜单",
    response_model=UnifiedResponse,
)
async def delete_menu(menu_id: int):
    """
    删除菜单
    """
    result = await menu_service.delete_menu(menu_id)
    if result is None:
        return UnifiedResponse(code=201, msg="菜单不存在", data={})
    else:
        return UnifiedResponse(data=result, msg="删除菜单成功")


@router.put(
    "/update_menu/{menu_id}",
    summary="更新菜单",
    response_model=UnifiedResponse[MenuOut],
)
async def update_menu(menu_id: int, menu_in: MenuIn):
    """更新菜单"""
    updated_menu = await menu_service.update_menu(menu_id, menu_in)
    if updated_menu is None:
        return UnifiedResponse(code=201, msg="菜单不存在", data={})
    return UnifiedResponse(data=updated_menu, msg="更新菜单成功")


@router.get(
    "/get_menu_tree",
    summary="获取菜单树",
    response_model=UnifiedResponse[List[MenuTree]],
)
async def get_menu_tree():
    """
    获取菜单列表
    """
    result = await MenuService.get_menu_tree()
    return UnifiedResponse(data=result)
